# -*- coding: UTF-8 -*-
__author__   = 'tommy'
__name__     = 'HTCUtils.py'
__license__  = 'htcharge.com@2016-08-29'
__version__  = 'v1.0.0'

import time, base64, re
import logging, logging.handlers
import time,datetime
K_ERR_SERVER    = '服务器错误'
K_ERR_NOUSER    = '没有找到该用户'
K_ERR_BALA      = '余额不足'
K_ERR_PERMIT    = '用户被禁用'
K_ERR_OPENDEV   = '设备打开失败'
K_ERR_STOP      = '停止失败，请重试'
K_ERR_NODEV     = '没有找到该设备'
K_ERR_NOORDER   = '没有找到该订单'
K_ERR_OVER      = '超过3次打印限制'
K_ERR_ERRCODE   = '验证码错误'
K_ERR_CARDNOTEXIST = '卡号不存在'

'''            if (tmpDBInfos == -1):
                return HTCMsg.msgError(-3, "数据库访问出错")
            if (tmpDBInfos == '0'):
                return HTCMsg.msgError(-2, "卡号不存在")
            if (tmpDBInfos == '-1'):
                return HTCMsg.msgError(-2, "传入参数为空")
            if (tmpDBInfos == '-2'):
                return HTCMsg.msgError(-2, "当前卡被禁用")
            if (tmpDBInfos == '-3'):
                return HTCMsg.msgError(-2, "当前卡被挂失")
            if (tmpDBInfos == '-4'):
                return HTCMsg.msgError(-2, "当前卡不存卡")
            if (tmpDBInfos == '-5'):
                return HTCMsg.msgError(-2, "主卡卡被禁用")
            if (tmpDBInfos == '-6'):
                return HTCMsg.msgError(-2, "主卡卡被挂失")
            if (tmpDBInfos == '-7'):
                return HTCMsg.msgError(-2, "主卡卡不存卡")'''

K_ERR_SRV               = '服务器错误'
K_ERR_PARAMS            = '参数错误'
K_ERR_USER_DISABLE      = '用户被禁用'
K_ERR_USER_NOTEXIST     = '用户不存在'
K_ERR_USER_LOST         = '该卡已挂失'


handler = logging.handlers.RotatingFileHandler('logs/water.log', maxBytes = 50*1024*1024, backupCount = 10)
formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s - %(message)s')
handler.setFormatter(formatter)
rctLogger = logging.getLogger('water')
rctLogger.addHandler(handler)
rctLogger.setLevel(logging.DEBUG)

def htprint(aFlag, aVal):
	if not aVal or len(aVal) <= 0 :
		print "%s-len=0,data="
		
	print "%s-len=%d,data=" % (aFlag,len(aVal)),
	for c in aVal:
		print hex(ord(c)),
	print

def timeUnix():
	return time.mktime(datetime.datetime.now().timetuple())
	
def timeDate():
	return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))

def hthex2str(aVal) :
    tmpStr = ''
    if aVal and len(aVal) > 0 :
        for c in aVal :
            tmpStr += "%02X " % ord(c)
    return tmpStr

def htcPrint(aMsg):
    tmpPrint = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    tmpPrint += " "
    tmpPrint += aMsg
    print tmpPrint

def isEmpty(aVal):
    if(not aVal) or (len(aVal) <= 0):
        return True
    return False

def base64Decode(aData):
    if (not aData or len(aData) <= 0) :
        return None
    tmpData = 4 - len(aData) % 4
    if tmpData :
        aData += b'='* tmpData
    return base64.b64decode(aData)

def base64Encode(aData):
    if (not aData or len(aData) <= 0) :
        return None
    return base64.b64encode(aData)
